import XtxConfirm from '@/components/xtx-confirm'
import Vue from 'vue'
const Confirm = Vue.extend(XtxConfirm)

export default (options) => {
  const deleteDom = () => {
    const domConfirm = document.querySelector('.xtx-confirm')
    domConfirm && document.body.removeChild(domConfirm)
  }
  return new Promise((resolve, reject) => {
    const confirm = new Confirm({ data: options })
    deleteDom()
    document.body.appendChild(confirm.$mount().$el)
    confirm.open()
    confirm.$on('on-cancel', () => {
      resolve(false)
      confirm.$destroy()
      deleteDom()
    })
    confirm.$on('on-submit', () => {
      resolve(true)
      confirm.$destroy()
      deleteDom()
    })
  })
}
